package com.yunyun400.vosk.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yunyun400.vosk.components.CallScheduler;

/**
 * 外呼控制器，提供API接口用于管理外呼任务
 */
@RestController
@RequestMapping("/api/call")
public class CallController {
    
    private static final Logger logger = LoggerFactory.getLogger(CallController.class);
    
    @Autowired
    private CallScheduler callScheduler;
    
    /**
     * 手动触发外呼任务
     */
    @RequestMapping("/trigger")
    public ResponseEntity<String> triggerCalls() {
        logger.info("接收到手动触发外呼任务的请求");
        
        try {
            // 异步执行外呼任务，避免阻塞API响应
            new Thread(() -> {
                callScheduler.triggerCalls();
            }).start();
            
            return ResponseEntity.ok("外呼任务已开始执行，请稍后查看日志确认结果");
        } catch (Exception e) {
            logger.error("触发外呼任务时发生错误", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("触发外呼任务失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取外呼服务状态
     */
    @RequestMapping("/status")
    public ResponseEntity<String> getStatus() {
        logger.info("接收到获取外呼服务状态的请求");
        
        try {
            // 这里可以返回更详细的状态信息
            // 例如：上次执行时间、执行结果、失败次数等
            return ResponseEntity.ok("外呼服务运行正常，定时任务将在每天凌晨5点执行");
        } catch (Exception e) {
            logger.error("获取外呼服务状态时发生错误", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("获取状态失败: " + e.getMessage());
        }
    }
}